package com.example.vv.model.setupItem.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.TreeEntity;
import com.example.vv.fragmentWork.view.FlowLayout;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SetupAdapter01 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<TreeEntity> treeEntities;
    Context context;

    public SetupAdapter01(List<TreeEntity> treeEntities, Context context) {
        this.treeEntities = treeEntities;
        this.context = context;
    }

    class MySetupHolder extends RecyclerView.ViewHolder {
        TextView item_flow_title;
        FlowLayout item_setup_flowLayout;
        public MySetupHolder(@NonNull View itemView) {
            super(itemView);
            item_flow_title = itemView.findViewById(R.id.item_flow_title);
            item_setup_flowLayout = itemView.findViewById(R.id.item_setup_flowLayout);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_setup_flowlayout, parent, false);
        MySetupHolder holder = new MySetupHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MySetupHolder)holder).item_flow_title.setText(treeEntities.get(position).getName());
        List<TreeEntity.ChildrenBean> children = treeEntities.get(position).getChildren();
        ((MySetupHolder) holder).item_setup_flowLayout.removeAllViews();
        for (int i = 0; i < children.size(); i++) {
            TextView flag  = new TextView(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT
                    , LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(5,5,5,5);
            flag.setLayoutParams(layoutParams);
            flag.setBackgroundResource(R.drawable.flag_01);
            flag.setText(treeEntities.get(position).getChildren().get(i).getName());
            ((MySetupHolder) holder).item_setup_flowLayout.addView(flag);
        }
    }

    @Override
    public int getItemCount() {
        return treeEntities.size();
    }
}
